import 'package:animated_tree_view/tree_view/tree_node.dart';
import 'package:animated_tree_view/tree_view/tree_view.dart';
import 'package:animated_tree_view/tree_view/widgets/expansion_indicator.dart';
import 'package:animated_tree_view/tree_view/widgets/indent.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'animated_tree_controller.dart';

class AnimatedTreePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<AnimatedTreeController>(
        init: AnimatedTreeController(),
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(title: Text('animated_tree')),
            body: TreeView.simpleTyped<Explorable, TreeNode<Explorable>>(
              tree: tree,
              showRootNode: false,
              expansionBehavior: ExpansionBehavior.scrollToLastChild,
              expansionIndicatorBuilder: (context, node) {
                // if (node.isRoot) {
                return PlusMinusIndicator(
                  tree: node,
                  alignment: Alignment.centerLeft,
                  color: Colors.grey[700],
                );
                // }

                return ChevronIndicator.rightDown(
                  tree: node,
                  alignment: Alignment.centerLeft,
                  color: Colors.grey[700],
                );
              },
              // indentation: const Indentation(), // 引导线
              builder: (context, node) => Padding(
                padding: const EdgeInsets.only(left: 16.0),
                child: ListTile(
                  title: Text(node.data?.name ?? "N/A"),
                  subtitle: Text(node.data?.createdAt.toString() ?? "N/A"),
                  leading: Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: node.icon,
                  ),
                ),
              ),
            ),
          );
        });
  }
}

extension on ExplorableNode {
  Icon get icon {
    if (isRoot) return const Icon(Icons.data_object);

    if (this is FolderNode) {
      if (isExpanded) return const Icon(Icons.folder_open);
      return const Icon(Icons.folder);
    }

    if (this is FileNode) {
      final file = data as File;
      if (file.mimeType.startsWith("image")) return const Icon(Icons.image);
      if (file.mimeType.startsWith("video"))
        return const Icon(Icons.video_file);
    }

    return const Icon(Icons.insert_drive_file);
  }
}

abstract class Explorable {
  final String name;
  final DateTime createdAt;

  Explorable(this.name) : createdAt = DateTime.now();

  @override
  String toString() => name;
}

class File extends Explorable {
  final String mimeType;

  File(super.name, {required this.mimeType});
}

class Folder extends Explorable {
  Folder(super.name);
}

typedef ExplorableNode = TreeNode<Explorable>;

typedef FileNode = TreeNode<File>;

typedef FolderNode = TreeNode<Folder>;

final tree = TreeNode<Explorable>.root(data: Folder("/root"))
  ..addAll([
    FolderNode(data: Folder("Documents"))
      ..addAll([
        FileNode(
          data: File("report.doc", mimeType: "application/msword"),
        ),
        FileNode(
          data: File("budget.xls", mimeType: "application/vnd.ms-excel"),
        ),
        FileNode(
          data: File("training.ppt", mimeType: "application/vnd.ms-powerpoint"),
        )
      ]),
    FolderNode(data: Folder("Media"))
      ..addAll([
        FolderNode(data: Folder("Pictures"))
          ..addAll([
            FileNode(data: File("birthday_1.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_2.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_3.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_4.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_5.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_6.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_7.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_8.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("birthday_9.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("lunch_1.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("lunch_2.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("lunch_3.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("lunch_4.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("lunch_5.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("lunch_6.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("lunch_7.jpg", mimeType: "image/jpeg")),
            FileNode(data: File("banner.png", mimeType: "image/png")),
          ]),
        FolderNode(data: Folder("Videos"))
          ..addAll([
            FolderNode(data: Folder("Birthday_23"))
              ..addAll([
                FileNode(
                    data: File("birthday_23_1.mp4", mimeType: "video/mp4")),
                FileNode(
                    data: File("birthday_23_2.mp4", mimeType: "video/mp4")),
              ]),
            FolderNode(data: Folder("vacation_ibiza"))
              ..addAll([
                FileNode(data: File("snorkeling.mp4", mimeType: "video/mp4")),
                FileNode(data: File("scuba.mp4", mimeType: "video/mp4")),
              ])
          ])
      ]),
    FolderNode(data: Folder("System"))
      ..addAll([
        FolderNode(data: Folder("temp")),
        FolderNode(data: Folder("apps"))
          ..addAll([
            FileNode(
              data: File("word.exe", mimeType: "application/win32_exe"),
            ),
            FileNode(
              data: File("powerpoint.exe", mimeType: "application/win32_exe"),
            ),
            FileNode(
              data: File("excel.exe", mimeType: "application/win32_exe"),
            ),
          ]),
        FileNode(
          data: File("sys.exe", mimeType: "application/win32_exe"),
        ),
        FileNode(
          data: File("config.exe", mimeType: "application/win32_exe"),
        )
      ]),
  ]);

// class UserName {
//   final String firstName;
//   final String lastName;
//
//   UserName(this.firstName, this.lastName);
// }
//
// final tree = TreeNode<UserName>.root(data: UserName("User", "Names"))
//   ..addAll([
//     TreeNode<UserName>(key: "0A", data: UserName("Sr. John", "Doe"))
//       ..add(TreeNode(key: "0A1A", data: UserName("Jr. John", "Doe"))),
//     TreeNode<UserName>(key: "0C", data: UserName("General", "Lee"))
//       ..addAll([
//         TreeNode<UserName>(key: "0C1A", data: UserName("Major", "Lee")),
//         TreeNode<UserName>(key: "0C1B", data: UserName("Happy", "Lee")),
//         TreeNode<UserName>(key: "0C1C", data: UserName("Busy", "Lee"))
//           ..addAll([
//             TreeNode<UserName>(key: "0C1C2A", data: UserName("Jr. Busy", "Lee"))
//           ]),
//       ]),
//     TreeNode<UserName>(key: "0D", data: UserName("Mr. Anderson", "Neo")),
//     TreeNode<UserName>(key: "0E", data: UserName("Mr. Smith", "Agent")),
//   ]);
